package com.gzsxy.esjy.service.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzsxy.esjy.common.base.exception.impl.CustomAssert;
import com.gzsxy.esjy.common.base.result.ResultCodeEnum;
import com.gzsxy.esjy.service.base.dto.request.VerifyRequest;
import com.gzsxy.esjy.service.base.dto.response.CategoryRsp;
import com.gzsxy.esjy.service.base.dto.response.PageResult;
import com.gzsxy.esjy.service.base.dto.response.VerifyRsp;
import com.gzsxy.esjy.service.product.dao.ProductMapper;
import com.gzsxy.esjy.service.product.dao.VerifyMappper;
import com.gzsxy.esjy.service.product.entity.Product;
import com.gzsxy.esjy.service.product.entity.Verify;
import com.gzsxy.esjy.service.product.entity.vo.VerifyQueryVo;
import com.gzsxy.esjy.service.product.service.VerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 商品审核
 * @date 2022/1/27 16:12
 */
@Service
public class VerifyServiceImpl  extends ServiceImpl<VerifyMappper, Verify> implements VerifyService {


    @Autowired
    private VerifyMappper verifyMappper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * @description: 查询审核商品
     * @param
     * @return
     */
    @Override
    public PageResult<VerifyRsp> getVerifyProduct(IPage<Product> pageParam, VerifyQueryVo verifyQueryVo) {

        IPage<VerifyRsp> verifyRspList =  verifyMappper.getVerifyProduct(pageParam,verifyQueryVo);
        List<VerifyRsp> records = verifyRspList.getRecords();
        //将商品的分类列表按照分类层级升序排列
        records.stream().map(verifyRsp -> verifyRsp.getCategoryList().stream().sorted(Comparator.comparing(CategoryRsp::getCategoryLevel)));
        PageResult<VerifyRsp> pageResult = new PageResult<>();
        pageResult.setRecords(records);
        pageResult.setTotal(verifyRspList.getTotal());
        return pageResult;
    }


    /**
     * @description: 根据审核id修改审核信息
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editVerify(VerifyRequest verifyRequest) {
        Verify verify = verifyMappper.selectById(verifyRequest.getId());
        //审核记录不存在
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(verify), ResultCodeEnum.VERIFY_NOT_EXIST_ERROR.getMessage());
        verify.setVerifyStatus(verifyRequest.getVerifyStatus());
        verify.setVerifyFeedback(verifyRequest.getVerifyFeedback());
        verifyMappper.updateById(verify);

        Product product = productMapper.selectById(verifyRequest.getProductId());
        //商品不存在
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(product), ResultCodeEnum.PRODUCT_NOT_EXIST_ERROR.getMessage());
        //给商品修改审核状态
        product.setVerifyStatus(verifyRequest.getVerifyStatus());
        //TODO 添加审核商品成色
        product.setConditions(verifyRequest.getConditions());
        productMapper.updateById(product);
    }
}
